package com.cxh.mchat.user.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cxh.mchat.common.constant.StatusConstant;
import com.cxh.mchat.common.security.bean.LoginUser;
import com.cxh.mchat.common.security.service.LoginService;
import com.cxh.mchat.user.bean.UserLoginReqBean;
import com.cxh.mchat.user.bean.UserRegisterReqBean;
import com.cxh.mchat.user.entity.User;
import com.cxh.mchat.user.mapper.UserMapper;
import com.cxh.mchat.user.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author cxh
 * @since 2021-12-27
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private LoginService loginService;

    @Override
    public User selectByUserNo(String userNo) {
        Assert.notBlank(userNo, "账号不能为空");
        return getBaseMapper().selectOne(new QueryWrapper<User>().eq("user_no", userNo));
    }

    @Override
    public User selectByEmail(String email) {
        Assert.notBlank(email, "邮箱地址不能为空");
        return getBaseMapper().selectOne(new QueryWrapper<User>().eq("email", email));
    }

    @Override
    public User register(UserRegisterReqBean reqBean) {
        Assert.isTrue(reqBean.getPassword().equals(reqBean.getPasswordAgain()), "两次密码输入不一致");

        User dbUser = selectByEmail(reqBean.getEmail());
        Assert.isNull(dbUser, "该邮箱已被注册，请直接登录。如忘记密码，可在登录页面进行重置");

        User user = new User();
        String userNo = System.currentTimeMillis() + "";//todo
        user.setUserNo(userNo);
        user.setNickName("用户" + userNo);
        user.setNickNamePinyin(PinyinUtil.getPinyin(user.getNickName()));
        user.setEmail(reqBean.getEmail());
        user.setPwd(passwordEncoder.encode(reqBean.getPassword()));
        user.setSalt(userNo);
        user.setStatus(StatusConstant.TRUE);
        user.setAvatar("user-01.png");

        boolean b = save(user);
        Assert.isTrue(b, "用户注册保存失败，稍后再试");

        return selectByUserNo(userNo);
    }

    @Override
    public LoginUser login(UserLoginReqBean reqBean) {

        String checkCode = reqBean.getCheckCode();
        Assert.isTrue(true, "验证码错误");//TODO

        User user = null;
        if(reqBean.getUserNo().contains("@")){
            user = selectByEmail(reqBean.getUserNo());
        }else {
            user = selectByUserNo(reqBean.getUserNo());
        }
        Assert.notNull(user, "账号不存在，请先注册");

        Assert.isTrue(passwordEncoder.matches(reqBean.getPassword(), user.getPwd()), "密码错误，请重新输入");

        return loginService.login(user.getUserNo(), reqBean.getPassword(), "", "");
    }
}
